﻿Imports Microsoft.Win32

Public Class OptionsUI

    Private Sub uxOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxOk.Click
        Dim config As New Configbase(Of Options)
        Dim settings As New Options
        With settings
            .ApplicationPath = Me.uxAppPath.Text
            .ExecuteOnStartUp = Me.uxExecuteOnStartup.Checked
            .Playsound = Me.uxPlaySound.Checked
            .TimeInterval = Me.uxMailCheckInterval.Value
            .WavFile = Me.uxBrowse.Tag.ToString
        End With
        config.SaveSettings(settings)
        Me.Close()
    End Sub

    Private Sub OptionsUI_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim config As New Configbase(Of Options)
        Dim settings As Options = config.ReadSettings
        With settings
            Me.uxMailCheckInterval.Value = .TimeInterval
            Me.uxExecuteOnStartup.Checked = .ExecuteOnStartUp
            Me.uxAppPath.Text = .ApplicationPath
            Me.uxPlaySound.Checked = .Playsound
            Me.uxBrowse.Enabled = .Playsound
            Me.uxBrowse.Text = IIf(.WavFile.Length <= 0, "No file selected", IO.Path.GetFileName(.WavFile))
            Me.uxBrowse.Tag = .WavFile
        End With
    End Sub

    Private Sub uxExecuteOnStartup_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxExecuteOnStartup.CheckedChanged
        'Dim RunonStartUpKey As RegistryKey = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
        Dim RunonStartUpKey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
        If uxExecuteOnStartup.Checked Then
            RunonStartUpKey.SetValue("MailNotify", String.Format("""{0}""", Application.ExecutablePath))
        Else
            RunonStartUpKey.DeleteValue("MailNotify")
        End If
    End Sub

    Private Sub uxBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxBrowse.Click
        Using opendialog As New OpenFileDialog
            With opendialog
                .AddExtension = True
                .Filter = "Wav Files(*.wav)|*.wav"
                .CheckFileExists = True
                .DefaultExt = "wav"
                .Multiselect = False
                .ReadOnlyChecked = False
                .ShowReadOnly = False
                .SupportMultiDottedExtensions = True
                .AutoUpgradeEnabled = False
                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    Me.uxBrowse.Text = IO.Path.GetFileName(.FileName)
                    Me.uxBrowse.Tag = .FileName
                End If
            End With
        End Using
    End Sub

    Private Sub uxPlaySound_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxPlaySound.CheckedChanged
        uxBrowse.Enabled = uxPlaySound.Checked
    End Sub
End Class